package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.User;
import com.woniu.mapper.UserMapper;
import com.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangjianbin
 * @since 2023-02-07 11:59:17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    UserMapper userMapper;
    @Override
    public void register(String username, String password, String repass) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if (user!=null){
            throw new RuntimeException("用户名已存在");
        }
        if (StringUtils.isEmpty(password)||StringUtils.isEmpty(repass)||!password.equals(repass)){
            throw new RuntimeException("两次密码输入不一致");
        }
        //校验通过，注册用户
        User newUser = new User();
        newUser.setUsername(username);
        newUser.setPassword(password);

        userMapper.insert(newUser);
    }

    @Override
    public void login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if (user==null){
            throw new RuntimeException("用户名不存在");
        }
        if (!user.getPassword().equals(password)){
            throw new RuntimeException("NI你输入的密码不正确");
        }
    }

    @Override
    public User getUserByUserName(String account) {
        //根据用户名查询用户
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",account);

        User user = userMapper.selectOne(queryWrapper);

        return user;
    }
}
